import {RECEIVE_ADDRESS, RECEIVE_CATEGORYS, RECEIVE_SHOPS,RECEIVE_USER_INFO,RESET_USER_INFO,RECEIVE_GOODS,RECEIVE_RATINGS,RECEIVE_INFO,INCREMENT_FOOD_COUNT ,DECREMENT_FOOD_COUNT,CLEAR_CART } from "./mutation-types";
import Vue from "vue";
export default {
  [RECEIVE_ADDRESS](state,data) {
    state.address=data;
  },
  [RECEIVE_CATEGORYS](state,data){
    state.categorys=data;
  },
  [RECEIVE_SHOPS](state,data){
    state.shops=data;
  },
  [RECEIVE_USER_INFO](state,data){
    state.userInfo=data;
    console.log(state.userInfo)
  },
  [RESET_USER_INFO](state){
    state.userInfo={};
  },
  [RECEIVE_GOODS](state,data){
    state.goods=data;
  },
  [RECEIVE_INFO](state,data){
    state.info=data;
  },
  [RECEIVE_RATINGS](state,data){
    state.ratings=data;
  },
  [INCREMENT_FOOD_COUNT](state,{food}){
    if(food.count){
      food.count++;
    }else{
      Vue.set(food,"count",1);
      state.cartFoods.push(food);
    }
  },
  [DECREMENT_FOOD_COUNT](state,{food}){
    if(food.count){
      food.count--;
      if(food.count==0){
        state.cartFoods.splice(state.cartFoods.indexOf(food),1);
      }
    }
  },
  //清空购物车
  [CLEAR_CART](state) {
    state.cartFoods.forEach(food=>food.count=0) //把后面追加的count也变为0
    state.cartFoods=[]  //清空购物车
  }
}
